
class SendTrack {
  constructor() {
    this.project = 'adomin-test-project'
    this.endpoint = 'cn-guangzhou.log.aliyuncs.com'
    this.logstore = 'adomin-test-store'
    // 用 阿里云 - 日志服务SLS 采集日志, 启用Web Tracking, PutWebtracking请求
    // https://help.aliyun.com/document_detail/31752.html?spm=5176.2020520112.0.0.2a5334c0XpOPDt
    this.url = `http://${this.project}.${this.endpoint}/logstores/${this.logstore}/track`
    this.xhr = new XMLHttpRequest;
  }
  send(data = {}) {
    // key不能做数字
    for (const key in data) {
      if (typeof data[key] === 'number') {
        data[key] = `${data[key]}`
      }
    }
    const bodyStr = JSON.stringify({__logs__: [data]})
    this.xhr.open('POST', this.url, true)
    this.xhr.setRequestHeader('Content-Type', 'application/json')
    this.xhr.setRequestHeader('x-log-apiversion', '0.6.0')
    this.xhr.setRequestHeader('x-log-bodyrawsize', bodyStr.length)
    this.xhr.send(bodyStr)
    this.xhr.onload = () => {
      // console.log(this.xhr.response)
    }
    this.xhr.onerror = (error) => {
      // console.log(error)
    }
  }
}
export default new SendTrack()